#include "HttpClient.h"

HttpClient::HttpClient(QObject *parent)
    : BasePort(parent)
{
    curl = NULL;
     naManager = new QNetworkAccessManager(this);

      QObject::connect(naManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
     // QNetworkConfiguration netConfig;
    //  netConfig.setConnectTimeout(5000);

     // qDebug()<<"naManager "<<naManager->s;
    curl = curl_easy_init();

    reconnectTimer = new QTimer(this);
    reconnectTimer->setInterval(5000);
    connect(reconnectTimer, SIGNAL(timeout()), this, SLOT(timeoutSlot()));
    reconnectTimeout=0;
   // reconnectTimer->start();
    reconnectTimer->setSingleShot(true);
}

void HttpClient::setPortParam(PortStruct portParam)
{

    m_portParam = portParam;
    this->setObjectName(portParam.name());
}

void HttpClient::open()
{

}

void HttpClient::close()
{

}

void HttpClient::write(PortStruct target,QByteArray data)
{

}

size_t cb_messageAuth_cert(void *data, size_t size, size_t nmemb, void *userp)
{
   size_t realsize = size * nmemb;//一次回调返回的数据量
    printf("\cb_messageAuth_cert   start response data :          %s\n",data);
    struct SeData *mem = (struct SeData *)userp;
    printf("nRt %d   realsize: %d\nd",mem->nRet,realsize);
    QByteArray array((char*)data);

   //  emit receiveDataSignal(m_portParam,array);
}


void HttpClient::get(PortStruct target,Package package)
{
    qDebug()<<"~~~~~in  HttpClient::get";
    QString head = "http://restapi.amap.com/v3/weather/weatherInfo?";
    QString body = package.body();
    head+=body;
   // request.setRawHeader("Content-Type","application/x-www-form-urlencoded");

     request.setUrl(QUrl::fromUserInput(head));


    // QByteArray byte_array;
    // byte_array.append(body.toLatin1());
     qDebug()<<head;
      naManager->get(request);//发起get请求
}
 void HttpClient::post(PortStruct target,Package package)
 {
     CURLcode res;
     struct SeData seenvdata;
/*
    Package package = sharedCmd->toPackage();
    QByteArray head=package.head();
    QByteArray data = package.body();

 QString _head = "http://172.18.5.78:80"+head;
 request.setUrl(QUrl::fromUserInput(_head));
  naManager->post(request,data);//发起post请求

 */
 qDebug()<<"~~~~~in  HttpClient::post";
/*
  if(curl)
  {
      seenvdata = package.seData();
      curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void*)&seenvdata);
      //curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0L);
     // curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L);

      curl_easy_setopt(curl,CURLOPT_CUSTOMREQUEST,"POST");
     // char url[256];
      QString url = "http://192.168.2.221:7078"+QString(package.head());
      // sprintf(url,"http://192.168.2.221:7078/registry/user/login");
     // QString url = "https://113.108.233.184:15443"+QString(package.head());
      qDebug()<<"curl  url: "<<url;
      curl_easy_setopt(curl,CURLOPT_URL,url.toLatin1().data());//.toStdString().c_str());

      curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,cb_messageAuth_cert);
      curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
      curl_easy_setopt(curl,CURLOPT_DEFAULT_PROTOCOL,"http");

      struct curl_slist *headers = NULL;
    //  headers = curl_slist_append(headers,"Expect:");
    //  headers = curl_slist_append(headers, "Authorization: Basic d3h0X3Rlc3Q6V1hUdGVzdDEx");
      headers = curl_slist_append(headers, "Content-Type: application/json");

      curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);
     // QString bodydata = "{\"username\":\"admin\",\"password\":\"88888888\"}";
      curl_easy_setopt(curl,CURLOPT_POSTFIELDS,package.body().data());

     // curl_easy_setopt(curl,CURLOPT_POSTFIELDS,bodydata.toLatin1().data());

     // printf("package.body.data is  %s\r\n",bodydata.toLatin1().data());

      res = curl_easy_perform(curl);
      qDebug()<<"curl_easy_perform code "<<res;




  }

  curl_easy_cleanup(curl);*/



 QString body = package.body();

 //request.setRawHeader("Expect:","");
 request.setRawHeader("Content-Type","application/json");
 QMap<QByteArray,QByteArray> map=package.HttpHead();
 QList<QByteArray> keys=map.keys();
 qDebug()<<"keys:"<<keys.count();

 foreach( QByteArray key,keys){
     request.setRawHeader(key,map.value(key));

     qDebug()<<"key:"<<key<<"value:"<<map.value(key);
 }
/*
for(auto it=map.begin();it!=map.end();++it){
     request.setRawHeader(it.key(),it.value());
     it++;
     qDebug()<<"key:"<<it.key()<<"value:"<<it.value();
}*/


// request.setRawHeader("Authorization","Basic d3h0X3Rlc3Q6V1hUdGVzdDEx");
 QString url = "http://192.168.2.221:7078"+QString(package.head());
  request.setUrl(QUrl::fromUserInput(url));




    // QString str("{\"userName\":\"admin\",\"cmd\":\"getPublicKey\"}");
     // QString str("{\"redirect\":\"%2F404\"}");
  //   QByteArray byte_array = str.toLatin1();

     QByteArray byte_array;

     byte_array.append(body.toLatin1());
     qDebug()<<"post body "<<byte_array;
     qDebug()<<"post "<<url;
     naManager->post(request,byte_array);//发起post请求

 }


void HttpClient::replyFinished(QNetworkReply *reply)
{
   qDebug()<<"reply->error "<< reply->error();
   QString allinfo = reply->readAll();
   QByteArray array = allinfo.toUtf8();

   // qDebug()<<"get json!!!!!!!"<<allinfo;

    qDebug()<<"get json raray!!!!!!!"<<array;


    emit receiveDataSignal(m_portParam,array);





}


void HttpClient::timeoutSlot()
{

}



HttpClient::~HttpClient()
{

}
